package com.kla.power.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kla.power.entity.AlarmEntity;
import com.kla.power.entity.InverterElectricEntity;
import com.kla.power.service.IAlarmService;
import com.kla.power.service.IInverterElectricService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 1:34 2025-02-09
 * @return
 */
@Component
@EnableScheduling
@Slf4j
public class AlarmScheduled {

    @Resource
    private IAlarmService iAlarmService;
    @Resource
    private IInverterElectricService iInverterElectricService;

    /**
     * 每隔10分钟查询一次
     */
    @Scheduled (cron = "0 0/5 * * * ?")
    public void alarm () {
        //1.每隔5分钟拿到所有报警的逆变器
        List<AlarmEntity> list = iAlarmService.list ( new LambdaQueryWrapper<AlarmEntity> ().eq ( AlarmEntity :: getStatus, 1 ).groupBy ( AlarmEntity :: getDeviceSn ) );
        //2.拿这些逆变器查询此时此刻前后各一个小时有误发电数据，如果有，则自动关闭状态，为已恢复
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now ();
        // 当前时间前一小时
        LocalDateTime oneHourBefore = now.minusHours ( 2 );
        // 当前时间后一小时
        LocalDateTime oneHourAfter = now.plusHours ( 2 );
        // 格式化日期时间为字符串以便输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss" );
        System.out.println ( "当前时间: " + now.format ( formatter ) );
        System.out.println ( "前一小时: " + oneHourBefore.format ( formatter ) );
        System.out.println ( "后一小时: " + oneHourAfter.format ( formatter ) );
        if ( list != null && list.size () > 0 ) {
            List<String> sn = list.stream ().map ( AlarmEntity :: getDeviceSn ).distinct ().collect ( Collectors.toList () );
            List<InverterElectricEntity> inverterElectricEntities = iInverterElectricService.list ( new LambdaQueryWrapper<InverterElectricEntity> ().
                    in ( InverterElectricEntity :: getSerialNumber, sn ).
                    ge ( InverterElectricEntity :: getCreateTime, oneHourBefore.format ( formatter ) ). //前面2小时
                    le ( InverterElectricEntity :: getCreateTime, oneHourAfter.format ( formatter ) ) ); //后面2小时
            System.out.println ( inverterElectricEntities );
            if ( inverterElectricEntities != null && inverterElectricEntities.size () > 0 ) {
                List<String> sns = inverterElectricEntities.stream ().map ( InverterElectricEntity :: getSerialNumber ).distinct ().collect ( Collectors.toList () );
                //修改报警
                iAlarmService.update ( new LambdaUpdateWrapper<AlarmEntity> ().set ( AlarmEntity :: getStatus, 0 ).in ( AlarmEntity :: getDeviceSn, sns ) );
            }
        }
    }

}
